| No. | Answer | Remark | |
|---|---|---|---|
| 1 | g | None of the Above | Methods m1(), m2(), m3(), m4(), m5(), and m6() throw subclasses of RuntimeException. Any exception that is a direct subclass of RuntimeException should not be caught and should not be declared in the throws clause of a method. |
| 2 | b | Prints: 0,1,1,0,0,1 | The nested catch block is able to catch a Level2Exception or any subclass of it causing b to be incremented. Both of the finally blocks are then executed. |
| 3 | d | Prints: D | This is a trick question. The expression (b = false) appears to be testing the value of b, but it is really setting the value of b. Notice that the equality operator has been replaced by the simple assignment operator. Anytime you think you see the equality operator on the exam make sure that it has not been replaced by the assignment operator. |
| 4 | f | Compiler Error | The throws clause of White.m1 declares a ColorException, but the catch clause in the main method catches only a subclass of ColorException. The result is a compiler error. |
| 5 | d | Prints: 0,1,1 | An exception is thrown so variable a is not incremented. Although Color.m1 declares a ColorException in the throws clause, a subclass of Color is free to declare only a subclass of Color in the throws clause of the overriding method. |
| 6 | d | Prints: 0,0,1,0,1,1 | The nested catch block is able to catch a Level2Exception or any subclass of it, but Exception is not a subclass of Level2Exception. The Exception is caught by the second of the two outer catch blocks causing f to be incremented. Both of the finally blocks are then executed. |
| 7 | c | Prints: C | This is a trick question. The boolean b is initialized to true, but the first if statement sets b to false. Notice that the equality operator has been replaced by the simple assignment operator. Anytime you think you see the equality operator on the exam make sure that it has not been replaced by the assignment operator. |
| 8 | f | Compiler Error | The throws clause of White.m2 declares a WhiteException, so the body of m2 may throw a WhiteException or any subclass of WhiteException. Instead, the body of m2 throws a superclass of WhiteException. The result is a compiler error. |
| 9 | e | Prints: 0,0,1,0,1 | The first catch block is able to catch a Level3Exception or any subclass of Level3Exception. The second catch block is able to catch a Level2Exception or any subclass of Level2Exception, so variable c is incremented. The finally block is also executed. |
| 10 | c | Prints: 0,1,1,0,0,1 | The nested catch block is able to catch a Level2Exception or any subclass of it causing b to be incremented. Both of the finally blocks are then executed. |
| 11 | g | Prints: 1,0,0,0,1 | No exception is thrown so variable a is incremented. The finally block is also executed. |
| 12 | c | Prints: C | This is a trick question. It appears that a compiler error would be generated as a result of attempting to use the value of variable b before it is initialized. In reality, the first if statement does the initialization of b. The expression (b = false) appears to be testing the value of b, but it is really setting the value of b. Notice that the equality operator has been replaced by the simple assignment operator. Anytime you think you see the equality operator on the exam make sure that it has not been replaced by the assignment operator. |
| 13 | d | Prints: 61433 | On the first pass through the loop the value of x is 6 so 5 is subtracted from x. On the second pass through the loop the value of x is 1 so 3 is added to the value of x. On the third pass through the loop the value of x is 4 so 1 is subtracted from the value of x. On the fourth pass through the loop the value of x is 3 so the variable, success, is incremented from zero to one. On the final pass the value of x is 3 and the variable, success, is incremented again to the value of 2. The boolean expression of the do while loop is now false so control flows out of the loop. |
| 14 | f | Compiler Error | A throw statement is the first statement in the outer try block. The switch statement that appears next is unreachable and generates a compile-time error. |
| 15 | d | Prints: 012345 | Although a program such as this will probably never be found in the real world, something similar is likely to be found on the real exam. This trick question has a do-loop nested inside of a while-loop. The body of each loop is a single statement rather than a block. The best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables involved in the loop. The printed results will provide the best possible explanation. The values of i and j for each iteration are as follows: (1,0)(1,1)(1,2)(1,3)(2,4)(3,5) . |
| 16 | e | Prints: 9,4 | The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The if statement causes the loop terminates when i reaches six. |
| 17 | b | Prints: 012 | Although a program such as this will probably never be found in the real world, something similar is likely to be found on the real exam. This trick question has a while-loop nested inside of a do-loop. The body of each loop is a single statement rather than a block. The best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows: (1,0,0)(2,0,1)(3,0,2). |
| 18 | c | Prints: 3,1 | On the first iteration case 1 is processed and control goes directly to the top of the for statement. The boolean expression of the do-loop is not processed. On the second iteration case 2 is processed and the break statement causes the switch statement to complete and control goes to the boolean expression of the do-loop. On the third iteration case 3 is processed and the break label2 statement cause the do-loop to complete abruptly without processing the boolean loop expression so j is not incremented. On the fourth iteration case 4 is processed and the break label 1 statement causes the outer loop to complete abruptly. |
| 19 | b | Prints: 1,3,2 | The case 2 statement is processed on the first iteration followed by case 4 and then the default case. Case 2 causes the switch statement to complete. Case 4 processes the continue label2 statement which causes control to transfer to the boolean expression of the do-loop. The default case causes control to transfer out of the outer for-loop. |